package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MiniComment;
import com.ca.ui.core.bean.PaperInfo;
import com.ca.ui.core.util.NumberUtils;
import com.ca.ui.core.util.PaperUtil;
import com.ca.ui.core.util.TimeUtils;
import com.ca.ui.core.view.button.IconButton;
import com.ca.ui.core.util.ImageUtils;

public class PaperCard extends ConstraintLayout {

    private PaperInfo paperInfo;

    public ImageView headerView;
    public TextView nameView;
    public ImageView downIcon;
    public TextView timeView;
    public ImageView carIcon;
    public TextView clubNameView;

    public TextView titleView;
    public PaperCardImageView imageView;
    public TextView contentView;
    public MiniCommentCard commentCard;

    public IconButton supportButton;
    public IconButton commentButton;
    public TextView moreView;

    public PaperCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public PaperCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public PaperCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.paper_card_layout, this);

        headerView = view.findViewById(R.id.iv_header);
        downIcon = view.findViewById(R.id.icon_down);
        nameView = view.findViewById(R.id.tv_name);
        timeView = view.findViewById(R.id.tv_time);
        carIcon = view.findViewById(R.id.icon_car);
        clubNameView = view.findViewById(R.id.tv_clubName);

        titleView = view.findViewById(R.id.tv_title);
        imageView = view.findViewById(R.id.imageView);
        contentView = view.findViewById(R.id.tv_content);

        commentCard = view.findViewById(R.id.card_comment);

        supportButton = view.findViewById(R.id.ib_support);
        commentButton = view.findViewById(R.id.ib_comment);
        moreView = view.findViewById(R.id.tv_more);

        headerView.setOnClickListener(v -> {});
        clubNameView.setOnClickListener(v -> {});
        downIcon.setOnClickListener(v -> {});

        paperInfo = new PaperInfo();

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PaperCard, 0, 0);


            paperInfo.setHeaderUrl(array.getString(R.styleable.PaperCard_pc_headerUrl));
            paperInfo.setName(array.getString(R.styleable.PaperCard_pc_name));
            paperInfo.setTime(array.getString(R.styleable.PaperCard_pc_time));
            paperInfo.setClubName(array.getString(R.styleable.PaperCard_pc_clubName));

            paperInfo.setTitle(array.getString(R.styleable.PaperCard_pc_title));
            paperInfo.setContent(array.getString(R.styleable.PaperCard_pc_content));

            MiniComment comment = new MiniComment();
            comment.setSupportCount((long) array.getInteger(R.styleable.PaperCard_pc_commentSupportCount, 0));
            comment.setName(array.getString(R.styleable.PaperCard_pc_commentName));
            comment.setContent(array.getString(R.styleable.PaperCard_pc_commentContent));
            paperInfo.setComment(comment);

            paperInfo.setSupportCount((long) array.getInteger(R.styleable.PaperCard_pc_supportCount, 0));
            paperInfo.setCommentCount((long) array.getInteger(R.styleable.PaperCard_pc_commentCount, 0));

            array.recycle();
        }

        setPaper(paperInfo);

        setOnClickListener(v -> {});
    }

    public PaperInfo getPaper() {
        return paperInfo;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void setPaper(PaperInfo paperInfo) {
        if (paperInfo == null) {
            paperInfo = new PaperInfo();
        }
        this.paperInfo = paperInfo;

        ImageUtils.loadCircleImage(getContext(), paperInfo.getHeaderUrl(), headerView);
        nameView.setText(paperInfo.getName());
        timeView.setText(TimeUtils.parseShowDateTime(paperInfo.getTime()));
        clubNameView.setText(paperInfo.getClubName());

        titleView.setText(paperInfo.getTitle());

        imageView.setImageUrlArray(paperInfo.getPaperImageUrlArray());

        //  解析XML文章内容
        contentView.setText(PaperUtil.parsePaperForText(paperInfo.getContent()));

        commentCard.setComment(paperInfo.getComment());

        supportButton.setValue(NumberUtils.numberToString(paperInfo.getSupportCount()));
        commentButton.setValue(NumberUtils.numberToString(paperInfo.getCommentCount()));
    }
}
